package com.xxh.dasongapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxh.dasongapp.common.entity.User;
import com.xxh.dasongapp.common.entity.UserRole;
import com.xxh.dasongapp.common.vo.UserVo;
import com.xxh.dasongapp.mapper.UserMapper;
import com.xxh.dasongapp.mapper.UserRoleMapper;
import com.xxh.dasongapp.service.RoleService;
import com.xxh.dasongapp.service.UserRoleService;
import com.xxh.dasongapp.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public Boolean saveUserAndRole(User user, Integer roleId) {
        //密码加密
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        save(user);
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getId());
        userRoleService.save(userRole);
        return true;
    }

    @Override
    public Boolean saveOrUpdateUserAndRole(User user, Integer roleId) {
        if (user.getPassword() != null){
            //密码加密
            String password = passwordEncoder.encode(user.getPassword());
            user.setPassword(password);
        }
        saveOrUpdate(user);
        UserRole userRole = new UserRole();
        UserRole oldUserRole = userRoleService.getByUserId(user.getId());
        userRole.setId(oldUserRole.getId());
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getId());
        userRoleService.saveOrUpdate(userRole);
        return true;
    }

    @Override
    public Boolean removeUserAndRoleById(String id) {
        removeById(id);
        UserRole userRole = userRoleService.getByUserId(id);
        userRoleService.removeById(userRole.getId());
        return true;
    }

    @Override
    public Page getUserAndRoleByPage(Page page, QueryWrapper<UserVo> queryWrapper) {
        List<UserVo> resources = userMapper.getUserAndRoleByPage(page,queryWrapper);
        page.setRecords(resources);
        return page;

    }

    @Override
    public UserVo getUserAndRoleById(String id) {
        UserVo userVo = new UserVo();
        Integer roleId = userRoleService.getByUserId(id).getRoleId();
        User user = getById(id);
        BeanUtils.copyProperties(user,userVo);
        userVo.setRoleId(roleId);
        return userVo;
    }

    @Override
    public UserVo getUserAndRoleByUsername(String username) {
        UserVo userVo = new UserVo();
        User user = getByUsername(username);
        Integer roleId = userRoleService.getByUserId(user.getId()).getRoleId();
        BeanUtils.copyProperties(user,userVo);
        userVo.setRoleId(roleId);

        return  userVo;
    }


    @Override
    public User getByOpenId(String openId) {
        return lambdaQuery().eq(User::getOpenId,openId)
                .one();
    }

    @Override
    public User getByUsername(String username) {
        return lambdaQuery().eq(User::getUsername,username)
                .one();
    }
}
